﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Xinyi.Common;
using Xinyi.Data;
using System.Collections;

namespace yfjs
{
    public partial class index : System.Web.UI.Page
    {
        /// <summary>
        /// 页面载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //是否跳转到首页
            if (Request.QueryString["writehtml"] == null)
            {
                Response.Redirect("en/index.html", true);
            }
            else
            {
                WriteHtmlFiles();
                Response.Write("网站生成完毕！<a href='../en/index.html' target='_blank'>查看</a>");
                Response.End();
            }
        }

        /// <summary>
        /// 生成所有html文件
        /// </summary>
        private void WriteHtmlFiles()
        {
            try
            {
                //删除所有.htm文件
                HtmlFiles.DelAllHtmlFiles("./");

                //定义路径变量，sql语句
                string strPath, strSql;

                strPath = "./template/";//路径变量赋值

                ArrayList arlFileNames = new ArrayList();//用于存储目录下的所有html文件名

                arlFileNames = HtmlFiles.ReadTemplateFileNames(strPath, "");//读取所有文件名

                //创建包含对象
                IncludeHtml myInclude = new IncludeHtml();

                //读取包含文件模板
                myInclude.HeadHtml = HtmlFiles.ReadTemplateFile(strPath + "include_head.html");
                myInclude.TopHtml = HtmlFiles.ReadTemplateFile(strPath + "include_top.html");
                myInclude.BottomHtml = HtmlFiles.ReadTemplateFile(strPath + "include_bottom.html");
                myInclude.FootHtml = HtmlFiles.ReadTemplateFile(strPath + "include_foot.html");
                myInclude.LeftHtml = HtmlFiles.ReadTemplateFile(strPath + "include_left.html");
                myInclude.File1Html = HtmlFiles.ReadTemplateFile(strPath + "include_file1.html");
                myInclude.File2Html = HtmlFiles.ReadTemplateFile(strPath + "include_file2.html");
                //英文
                myInclude.HeadHtml_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_head.html");
                myInclude.TopHtml_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_top.html");
                myInclude.BottomHtml_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_bottom.html");
                myInclude.FootHtml_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_foot.html");
                myInclude.LeftHtml_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_left.html");
                myInclude.File1Html_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_file1.html");
                myInclude.File2Html_En = HtmlFiles.ReadTemplateFile(strPath + "en/include_file2.html");

                //打开数据库连接
                DataClass myData = new DataClass();
                SqlConnection myConn = myData.ConnOpen();

                //定义sql语句
                strSql = "select * from T_Book order by ID desc;"
                    + "select * from T_Info order by SID,id desc;"
                    + "select * from V_Link order by SID,id desc;"
                    + "select * from T_LinkType order by SID,id desc;"
                    + "select * from V_News order by SID,id desc;"
                    + "select * from T_NewType order by SID,id desc;"
                    + "select * from V_Products order by SID,id desc;"
                    + "select * from T_ProType order by SID,id desc;"
                    + "select * from T_SEO order by ID";

                //获取数据集
                DataSet myDs = myData.GetDataSet(strSql, myConn);

                this.WriteHtmlFilesByDir(strPath, arlFileNames, myInclude, myDs);

                //关闭数据库释放资源
                myDs.Dispose();
                myData.ConnClose(myConn);
            }
            catch
            {
                Response.Write("生成静态页面失败！请联系程序员！");
            }
        }

        /// <summary>
        /// 按目录读取模板文件并生成html文件
        /// </summary>
        /// <param name="strPath">文件路径</param>
        /// <param name="arlFileNames">文件名数组</param>
        /// <param name="myInclude">包含文件对象</param>
        /// <param name="myDs">数据集</param>
        private void WriteHtmlFilesByDir(string strPath, ArrayList arlFileNames, IncludeHtml myInclude, DataSet myDs)
        {
            string strFileName, strHtml, strURL;

            //循环读取所有文件名
            for (int f = 0; f < arlFileNames.Count; f++)
            {
                strFileName = arlFileNames[f].ToString();//获取文件名

                //判断是否包含文件，排除包含文件读取
                if (strFileName.IndexOf("include_") == -1)
                {
                    ArrayList arlPageContent = new ArrayList();

                    //定义临时视图对象（用于筛选数据读取）
                    DataView myDv = null;

                    strHtml = HtmlFiles.ReadTemplateFile(strPath + strFileName);//获取html、文件

                    //替换包含文件
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:head$", myInclude.HeadHtml);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:top$", myInclude.TopHtml);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:bottom$", myInclude.BottomHtml);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:foot$", myInclude.FootHtml);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:left$", myInclude.LeftHtml);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:file1$", myInclude.File1Html);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:file2$", myInclude.File2Html);
                    //英文
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/head$", myInclude.HeadHtml_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/top$", myInclude.TopHtml_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/bottom$", myInclude.BottomHtml_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/foot$", myInclude.FootHtml_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/left$", myInclude.LeftHtml_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/file1$", myInclude.File1Html_En);
                    strHtml = HtmlFiles.ReplaceContent(strHtml, "$include:en/file2$", myInclude.File2Html_En);

                    //替换全局变量，存储在webconfig中
                    string strGlobal_Url = System.Configuration.ConfigurationManager.AppSettings["WebSiteUrl"];
                    if (strGlobal_Url != null)
                        strHtml = HtmlFiles.ReplaceContent(strHtml, "$global:url$", strGlobal_Url);

                    //获取本页面自定义标签
                    CustomTags myTags = new CustomTags(strHtml);
                    ArrayList arlTags = myTags.Objects;

                    //seo优化字段替换
                    myDv = new DataView(myDs.Tables[8], "", "", DataViewRowState.CurrentRows);
                    for (int i = 0; i < myDv.Count; i++)
                    {
                        //判断文件名是否存在
                        if (("/" + strFileName.ToLower())==("/" + myDv[i]["PageName"].ToString().ToLower()))
                        {
                            strHtml = HtmlFiles.ReplaceContent(strHtml, "$seo:title$", myDv[i]["title"].ToString());
                            strHtml = HtmlFiles.ReplaceContent(strHtml, "$seo:keywords$", myDv[i]["keywords"].ToString());
                            strHtml = HtmlFiles.ReplaceContent(strHtml, "$seo:description$", myDv[i]["description"].ToString());
                            strHtml = HtmlFiles.ReplaceContent(strHtml, "$seo:author$", myDv[i]["author"].ToString());
                        }
                    }
                    myDv.Dispose();

                    //页面信息替换
                    myDv = new DataView(myDs.Tables[1], "", "", DataViewRowState.CurrentRows);
                    for (int i = 0; i < myDv.Count; i++)
                    {
                        strHtml = HtmlFiles.ReplaceContent(strHtml, "$info:" + myDv[i]["ID"].ToString() + "$", myDv[i]["Memo"].ToString());//ID确定
                        strHtml = HtmlFiles.ReplaceContent(strHtml, "$info:" + myDv[i]["Called"].ToString() + "$", myDv[i]["Memo"].ToString());//名称确定
                    }
                    myDv.Dispose();

                    //添加到分页对象中
                    PageContent myPage = new PageContent();
                    myPage.HTML = strHtml;
                    myPage.FileName = strFileName;
                    arlPageContent.Add(myPage);

                    //获取本页面所有标签，并替换
                    for (int i = 0; i < arlTags.Count; i++)
                    {
                        //对象赋值
                        CustomTag myTag = (CustomTag)arlTags[i];

                        //获取连接地址
                        strURL = strFileName.Split('.')[0].ToString() + "_" + myTag.TagType + "_" + myTag.TypeID + "_{page}." + strFileName.Split('.')[1].ToString();

                        ArrayList arlTemp = null;//临时数组

                        for (int y = 0; y < arlPageContent.Count; y++)
                        {
                            //友情链接替换
                            if (myTag.TagType == "links")
                            {
                                //判断是否首页
                                if (strFileName.IndexOf("index.htm") > -1)
                                {
                                    arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[2], myDs.Tables[3], (PageContent)arlPageContent[y], "TypeID",
                                        "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);
                                }
                                else
                                {
                                    arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[2], myDs.Tables[3], (PageContent)arlPageContent[y], "TypeID",
                                        "PID", "TypeCalled", " and IsShowOther=1", strURL, myTag.WriteOldFile);
                                }
                            }

                            //新闻替换
                            if (myTag.TagType == "news")
                            {
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[4], myDs.Tables[5], (PageContent)arlPageContent[y],
                                    "NewType", "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);

                                //分类页面新闻列表
                                if (myTag.TypeID > 0)
                                    myDv = new DataView(myDs.Tables[5], "IsShow=1 and (ID=" + myTag.TypeID + " or PID=" + myTag.TypeID
                                        + ")", "", DataViewRowState.CurrentRows);
                                else
                                    myDv = new DataView(myDs.Tables[5], "IsShow=1", "", DataViewRowState.CurrentRows);
                                for (int x = 0; x < myDv.Count; x++)
                                {
                                    //定义临时数组变量
                                    ArrayList arlTemp1 = new ArrayList();

                                    myTag.TypeID = Convert.ToInt32(myDv[x]["ID"].ToString());

                                    //重新获取连接地址
                                    strURL = strFileName.Split('.')[0].ToString() + "_" + myTag.TagType + "_" + myTag.TypeID + "_{page}." + strFileName.Split('.')[1].ToString();

                                    //全局分类值
                                    //((PageContent)arlPageContent[y]).HTML = ((PageContent)arlPageContent[y]).HTML.Replace("$global:newtype$", myTag.TypeID.ToString());
                                    //获取网页数据
                                    arlTemp1 = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[4], myDs.Tables[5], (PageContent)arlPageContent[y],
                                        "NewType", "PID", "TypeCalled", "", strURL, false);

                                    if (arlTemp != null && arlTemp1 != null)
                                        arlTemp.AddRange(arlTemp1);

                                    arlTemp1 = null;
                                }
                                myDv.Dispose();
                            }

                            //新闻分类
                            if (myTag.TagType == "newtype")
                            {
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[5], myDs.Tables[5], (PageContent)arlPageContent[y],
                                    "PID", "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);

                                //替换嵌套子标签
                                //this.ReplaceSubTags(arlTemp, myDs.Tables[4], myDs.Tables[5], (PageContent)arlPageContent[y], "NewType");
                            }

                            //新闻内容页
                            if (myTag.TagType == "newinfo")
                            {
                                //获取连接地址
                                strURL = strFileName.Split('.')[0].ToString() + "_{page}." + strFileName.Split('.')[1].ToString();
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[4], myDs.Tables[5], (PageContent)arlPageContent[y],
                                    "NewType", "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);
                            }

                            //产品替换
                            if (myTag.TagType == "products")
                            {
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[6], myDs.Tables[7], (PageContent)arlPageContent[y],
                                    "TypeID", "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);

                                //分类页面产品列表
                                myDv = new DataView(myDs.Tables[7], "IsShow=1", "", DataViewRowState.CurrentRows);
                                for (int x = 0; x < myDv.Count; x++)
                                {
                                    //定义临时数组变量
                                    ArrayList arlTemp1 = new ArrayList();

                                    myTag.TypeID = Convert.ToInt32(myDv[x]["ID"].ToString());

                                    //重新获取连接地址
                                    strURL = strFileName.Split('.')[0].ToString() + "_" + myTag.TagType + "_" + myTag.TypeID + "_{page}." + strFileName.Split('.')[1].ToString();
                                    //获取网页数据
                                    arlTemp1 = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[6], myDs.Tables[7], (PageContent)arlPageContent[y],
                                        "TypeID", "PID", "TypeCalled", "", strURL, false);

                                    if (arlTemp != null && arlTemp1 != null)
                                        arlTemp.AddRange(arlTemp1);

                                    arlTemp1 = null;
                                }
                                myDv.Dispose();
                            }

                            //新闻分类
                            if (myTag.TagType == "protype")
                            {
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[7], myDs.Tables[7], (PageContent)arlPageContent[y], "PID",
                                    "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);
                            }

                            //新闻内容页
                            if (myTag.TagType == "proinfo")
                            {
                                //获取连接地址
                                strURL = strFileName.Split('.')[0].ToString() + "_{page}." + strFileName.Split('.')[1].ToString();
                                arlTemp = this.GetAllPagesHtml(myTags, myTag, myDs.Tables[6], myDs.Tables[7], (PageContent)arlPageContent[y],
                                    "TypeID", "PID", "TypeCalled", "", strURL, myTag.WriteOldFile);
                            }
                        }

                        //临时数组不为空的时候，添加到主数组中
                        if (arlTemp != null)
                            arlPageContent.AddRange(arlTemp);
                    }

                    for (int i = 0; i < arlPageContent.Count; i++)
                    {
                        PageContent myPageTemp = (PageContent)arlPageContent[i];

                        //替换嵌套子标签
                        this.ReplaceSubTags(myDs, myPageTemp);

                        HtmlFiles.WriteHtmlFile(myPageTemp.FileName, myPageTemp.HTML);
                    }
                }
            }
        }

        /// <summary>
        /// 替换标签
        /// </summary>
        /// <param name="myTags">标签对象数组</param>
        /// <param name="myTag">标签对象</param>
        /// <param name="myDv">数据视图对象</param>
        /// <param name="strHtml">html</param>
        /// <param name="intPage">分页，当前页</param>
        /// <param name="strURL">分页url{page}</param>
        /// <returns>替换后的html</returns>
        private string ReplaceTag(CustomTags myTags, CustomTag myTag, DataView myDv, string strHtml, int intPage, string strURL)
        {
            string strTemp, strTemp2, strResult;

            int intCycle = 0;//循环次数
            int intCurrent = 0;//数据游标

            strResult = strHtml;

            //循环输出
            if (myTag.Cycle == -1)
            {
                intCurrent = 0;
                intCycle = myDv.Count;
            }
            else
            {
                intCycle = myTag.Cycle;

                //判断是否分页
                if (intPage > 0 && myTag.PageID > -1)
                {
                    intCurrent = (intPage - 1) * intCycle;
                    intCycle += intCurrent;
                }
                else
                    intCurrent = 0;
            }

            //调用获取替换标签
            strTemp = this.RepalceSubTag(intCurrent, intCycle, myDv, myTag, myTag.TypeID.ToString());

            if (strTemp == "") strTemp = "<div id='nodata'>No Data!</div>";

            //处理分页显示栏
            if (intPage > 0 && myTag.PageID > -1)
            {
                strTemp2 = myTags.OutputPage(intPage, myDv.Count, myTag.PageCount, myTag, strURL);

                //替换分页按钮信息
                strHtml = HtmlFiles.ReplaceContent(strHtml, "$page:" + myTag.PageID + "$", strTemp2);
                //替换总数
                strHtml = HtmlFiles.ReplaceContent(strHtml, "$page:" + myTag.PageID + ":rscount$", myDv.Count.ToString());
                //替换当前页
                strHtml = HtmlFiles.ReplaceContent(strHtml, "$page:" + myTag.PageID + ":page$", intPage.ToString());
                //替换总页数
                strHtml = HtmlFiles.ReplaceContent(strHtml, "$page:" + myTag.PageID + ":pagecount$", myTag.PageCount.ToString());
                //替换一页的个数
                strHtml = HtmlFiles.ReplaceContent(strHtml, "$page:" + myTag.PageID + ":pagenum$", intCycle.ToString());
            }

            strResult = HtmlFiles.ReplaceContent(strHtml, myTag.OriginalTag, strTemp);

            //替换分类选中标签
            strResult = this.ReplaceSelTypeID(myTag, strResult, strURL);

            return strResult;
        }

        /// <summary>
        /// 替换子标签
        /// </summary>
        /// <param name="intCurrent">当前游标</param>
        /// <param name="intCycle">循环次数</param>
        /// <param name="myDv">数据表</param>
        /// <param name="myTag">自定义标签对象</param>
        /// <param name="strTypeID">本层次PID</param>
        /// <param name="strURL">链接地址，用于判断分类选中</param>
        /// <returns>返回递归后的html代码</returns>
        private string RepalceSubTag(int intCurrent, int intCycle, DataView myDv, CustomTag myTag, string strTypeID)
        {
            string strTemp, strTemp1, strTemp2, strPID, strID;

            strTemp = "";
            strPID = "";
            strID = "";

            int intRNum = 0;//行个数计数器

            //数据行循环
            for (int x = intCurrent; x < intCycle; x++)
            {
                intRNum++;

                strTemp2 = myTag.OutputTag;

                //数据列循环，主要是获取列名
                for (int y = 0; y < myDv.Table.Columns.Count; y++)
                {
                    //列名赋值
                    strTemp1 = myDv.Table.Columns[y].ColumnName;

                    //判断是否超出数据范围
                    if (x >= myDv.Count)
                    {
                        strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + strTemp1.ToLower() + "$", "");
                        strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$rowid$", "");//行统计个数id
                    }
                    else
                    {
                        string strTemp3 = myDv[x][strTemp1].ToString();//赋值

                        //赋值PID，如果有的话
                        if (strTemp1 == "PID")
                            strPID = myDv[x][strTemp1].ToString();
                        if (strTemp1 == "ID")
                            strID = myDv[x][strTemp1].ToString();
                        if (strTemp1.ToLower() == "newpic" || strTemp1.ToLower() == "propic" || strTemp1.ToLower() == "pic")
                            strTemp3 = FunctionClass.GetUploadFileRelativeURL(strTemp3);

                        //处理日期转换格式
                        if (myTag.DateTimeStr != "")
                        {
                            string[] arrDateTimeStr = myTag.DateTimeStr.Split('|');
                            string[] arrDateTimeFormat = myTag.DateTimeFormat.Split('|');

                            //循环所有日期字段
                            for (int op = 0; op < arrDateTimeStr.Length; op++)
                            {
                                //判断列名称是否一致
                                if (arrDateTimeStr[op].ToLower() == strTemp1.ToLower())
                                {
                                    strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + strTemp1.ToLower() + "$",
                                        Convert.ToDateTime(strTemp3).ToString(arrDateTimeFormat[op]));
                                }
                            }
                        }

                        //处理其他字符串转换格式
                        if (myTag.StringCellNames != "")
                        {
                            string[] arrStringCellNames = myTag.StringCellNames.Split('|');
                            string[] arrStringFormat = myTag.StringFormat.Split('|');

                            //循环所有字段
                            for (int op = 0; op < arrStringCellNames.Length; op++)
                            {
                                //判断列名称是否一致
                                if (arrStringCellNames[op].ToLower() == strTemp1.ToLower())
                                {
                                    string[] arrTTs = arrStringFormat[op].Split('^');
                                    //内容循环
                                    for (int opt = 0; opt < arrTTs.Length; opt++)
                                    {
                                        //判断是否值对的上
                                        if (arrTTs[opt].IndexOf(strTemp3.ToLower() + ":") > -1)
                                            strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + strTemp1.ToLower() + "$",
                                                arrTTs[opt].Replace(strTemp3.ToLower() + ":", ""));
                                    }
                                }
                            }
                        }

                        //判断如果是关键词字段
                        if (strTemp1.ToLower() == "keywords")
                        {
                            //获取搜索关键词列表
                            string strTempFileName = myTag.TagType;
                            if (strTempFileName.IndexOf("proinfo") > -1)
                                strTempFileName = "products";
                            if (strTempFileName.IndexOf("newinfo") > -1)
                                strTempFileName = "news";

                            strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + strTemp1.ToLower() + "$", this.GetKeyWordsLinks(strTemp3, "Search_" + strTempFileName + ".aspx?"));
                        }
                        else
                            strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + strTemp1.ToLower() + "$", strTemp3);

                        strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$" + myTag.TagType + ":" + strTemp1.ToLower() + "$", strTemp3);
                        strTemp2 = HtmlFiles.ReplaceContent(strTemp2, "$rowid$", intRNum.ToString());//行统计个数id
                    }
                }

                //判断是否分类列表
                if (myTag.TagType.IndexOf("type") > -1 && strPID != "" && strID != "")
                {
                    //判断是否本层次的分类
                    if (strTypeID == strPID)
                    {
                        //加如分类层样式
                        strTemp2 = "<div class=\"" + myTag.TagType + "_" + strPID + "$sel" + myTag.TagType + "Class:" + strID + "$\">" + strTemp2 + "</div>";

                        //判断是否显示子分类
                        if (myTag.SubType)
                        {
                            //筛选下级分类数据
                            DataView myDv1 = new DataView(myDv.Table, myDv.RowFilter.Replace("PID=" + myTag.TypePID, "PID=" + strID), "", DataViewRowState.CurrentRows);
                            //递归调用获取下级分类
                            strTemp2 += this.RepalceSubTag(intCurrent, myDv1.Count, myDv1, myTag, strID);
                            myDv1.Dispose();
                        }
                    }
                    else
                        strTemp2 = "";
                }

                //判断是否超出数据范围
                if (x >= myDv.Count)
                {
                    //判断是否输出空行
                    if (myTag.ShowSpace)
                    {
                        //判断是否有换行标签
                        if (myTag.RowWarp > 0)
                        {
                            //判断是否最后一行
                            if ((double)(x + 1) / myTag.RowWarp > (double)myTag.Cycle / myTag.RowWarp - 1)
                                strTemp2 = strTemp2.Replace("class=\"main_2\"", "class=\"main_2_d\"");

                            if (x % myTag.RowWarp == 0)//判断是否每行第一个
                            {
                                if (strTemp2 != "")//内容不为空的时候才添加标记
                                    strTemp += (myTag.RowWarpStart + strTemp2);
                            }
                            else if ((x + 1) % myTag.RowWarp == 0)//判断是否每行最后一个
                            {
                                intRNum = 0;
                                if (strTemp2 != "")//内容不为空的时候才添加标记
                                    strTemp += ((myTag.RowWarpReplace == "" ? strTemp2 : strTemp2.Replace(myTag.RowWarpReplace, "")) + myTag.RowWarpEnd);
                            }
                            else
                                strTemp += strTemp2;
                        }
                        else
                            strTemp += strTemp2;
                    }
                    else
                    {
                        //判断是否有换行标签
                        if (myTag.RowWarp > 0)
                        {
                            if (x > 0 && x == myDv.Count)//判断是否最后一条数据
                            {
                                if (strTemp2 != "")//内容不为空的时候才添加标记
                                    strTemp += myTag.RowWarpEnd;
                            }
                        }
                    }
                }
                else
                {
                    //判断是否有换行标签
                    if (myTag.RowWarp > 0)
                    {
                        //判断是否最后一行
                        if ((double)(x + 1) / myTag.RowWarp > (double)myDv.Count / myTag.RowWarp - 1)
                            strTemp2 = strTemp2.Replace("class=\"main_2\"", "class=\"main_2_d\"");

                        if (x % myTag.RowWarp == 0)//判断是否每行第一个
                        {
                            if (strTemp2 != "")//内容不为空的时候才添加标记
                                strTemp += (myTag.RowWarpStart + strTemp2);
                        }
                        else if ((x + 1) % myTag.RowWarp == 0)//判断是否每行最后一个
                        {
                            intRNum = 0;
                            if (strTemp2 != "")//内容不为空的时候才添加标记
                                strTemp += ((myTag.RowWarpReplace == "" ? strTemp2 : strTemp2.Replace(myTag.RowWarpReplace, "")) + myTag.RowWarpEnd);
                        }
                        else
                            strTemp += strTemp2;
                    }
                    else
                        strTemp += strTemp2;
                }
            }

            return strTemp;
        }

        /// <summary>
        /// 获取所有分页html
        /// </summary>
        /// <param name="myTags">标签对象数组</param>
        /// <param name="myTag">标签对象</param>
        /// <param name="tableList">数据表</param>
        /// <param name="tableType">分类表</param>
        /// <param name="myPage">分页内容对象</param>
        /// <param name="strCellName">sql语句中的列名称</param>
        /// <param name="strPCellName">数据表中的列名称</param>
        /// <param name="strCalledName">分类名称字段名</param>
        /// <param name="strWhere">附加条件语句</param>
        /// <param name="strURL">分页url{page}</param>
        /// <param name="WriteOldFileName">分页显示列表，显示原有文件名</param>
        /// <returns>分页对象数组</returns>
        private ArrayList GetAllPagesHtml(CustomTags myTags, CustomTag myTag, System.Data.DataTable tableList, System.Data.DataTable tableType,
            PageContent myPage, string strCellName, string strPCellName, string strCalledName, string strWhere, string strURL, bool WriteOldFileName)
        {
            string strOrder = "";

            string strHtml = myPage.HTML;

            ArrayList arlPageContent = null;

            //获取输出字符串
            myTags.GetOutputString(myTag, tableType, strCellName, strPCellName, strCalledName);

            //where
            if (myTag.OutputWhere == "" && strWhere != "")
                strWhere = strWhere.Replace(" and ", "");
            if (myTag.OutputWhere.IndexOf(" and ") == 0)
                myTag.OutputWhere = myTag.OutputWhere.Substring(5);

            //排序，判断显示分类列表时候的排序和普通排序
            if (myTag.TagType.IndexOf("type") > -1)
                strOrder = "PID,SID,ID";
            else
                strOrder = myTag.OutputOrder;

            //筛选数据
            DataView myDv = new DataView(tableList, myTag.OutputWhere + strWhere, strOrder, DataViewRowState.CurrentRows);

            //判断是否有分页标签id
            if (myTag.PageID > -1)
            {
                //计算页数
                double dbPageCount = 0;
                dbPageCount = Math.Ceiling((double)myDv.Count / (double)myTag.Cycle);
                if (dbPageCount <= 0) dbPageCount = 1;

                //赋值页数到对象属性中
                myTag.PageCount = (int)dbPageCount;

                arlPageContent = new ArrayList();

                for (int i = 1; i <= myTag.PageCount; i++)
                {
                    PageContent myPageContent = new PageContent();
                    myPageContent.HTML = this.ReplaceTag(myTags, myTag, myDv, strHtml.Replace("$global:newtype$", myTag.TypeID.ToString()), i, strURL);
                    myPageContent.FileName = strURL.Replace("{page}", i.ToString());
                    arlPageContent.Add(myPageContent);

                    //第一条数据重复记录源文件
                    if (i == 1 && WriteOldFileName)
                    {
                        PageContent myPageContent1 = new PageContent();
                        myPageContent1.HTML = this.ReplaceTag(myTags, myTag, myDv, strHtml.Replace("$global:newtype$", myTag.TypeID.ToString()), i, strURL);
                        myPageContent1.FileName = myPage.FileName;
                        arlPageContent.Add(myPageContent1);
                    }
                }

            }
            else if (myTag.TagType == "newinfo" || myTag.TagType == "proinfo")//如果是新闻显示页面
            {
                arlPageContent = new ArrayList();

                for (int i = 0; i < myDv.Count; i++)
                {
                    string strNewHtml = strHtml.Clone().ToString();

                    //筛选当前记录
                    DataView myDv1 = new DataView(tableList, myTag.OutputWhere + " and ID=" + myDv[i]["ID"].ToString(), myTag.OutputOrder,
                        DataViewRowState.CurrentRows);

                    PageContent myPageContent = new PageContent();

                    if (myDv1.Count > 0)
                    {
                        //info信息显示页面特殊seo信息
                        strNewHtml = HtmlFiles.ReplaceContent(strNewHtml, "$seo:title$", myDv1[0]["Seo_Title"].ToString());
                        strNewHtml = HtmlFiles.ReplaceContent(strNewHtml, "$seo:keywords$", myDv1[0]["Seo_KeyWords"].ToString());
                        strNewHtml = HtmlFiles.ReplaceContent(strNewHtml, "$seo:description$", myDv1[0]["Seo_Description"].ToString());
                        strNewHtml = HtmlFiles.ReplaceContent(strNewHtml, "$seo:author$", myDv1[0]["Seo_Author"].ToString());
                    }

                    myPageContent.HTML = this.ReplaceTag(myTags, myTag, myDv1, strNewHtml, i, strURL);
                    myPageContent.FileName = strURL.Replace("{page}", myDv[i]["ID"].ToString());
                    arlPageContent.Add(myPageContent);

                    myDv1.Dispose();
                }
            }
            else
            {
                myPage.HTML = this.ReplaceTag(myTags, myTag, myDv, strHtml, 0, strURL);
            }

            //释放数据视图对象
            myDv.Dispose();

            return arlPageContent;
        }

        /// <summary>
        /// 根据关键词获取连接
        /// </summary>
        /// <param name="strKeyWords">关键词</param>
        /// <param name="strLinks">连接地址</param>
        /// <returns>返回带连接的关键词</returns>
        private string GetKeyWordsLinks(string strKeyWords, string strLinks)
        {
            string strResult = "";
            string[] arrResult = null;

            if (strKeyWords.IndexOf(";") > -1)
            {
                arrResult = strKeyWords.Split(';');
            }
            else if (strKeyWords.IndexOf("；") > -1)
            {
                arrResult = strKeyWords.Split('；');
            }
            else if (strKeyWords.IndexOf("|") > -1)
            {
                arrResult = strKeyWords.Split('|');
            }
            else if (strKeyWords.IndexOf(",") > -1)
            {
                arrResult = strKeyWords.Split(',');
            }
            else if (strKeyWords.IndexOf("，") > -1)
            {
                arrResult = strKeyWords.Split('，');
            }
            else if (strKeyWords.IndexOf(" ") > -1)
            {
                arrResult = strKeyWords.Split(' ');
            }
            else
            {
                arrResult = strKeyWords.Split(',');
            }

            for (int i = 0; i < arrResult.Length; i++)
            {
                if (arrResult[i].ToString() != "")
                {
                    strResult += "<a href=\"" + strLinks + "keyword=" + FunctionClass.UrlEncode(arrResult[i]) + "\">" + arrResult[i]
                        + "</a>&nbsp;|&nbsp;";
                }
            }

            //去除末尾
            if (strResult.Length > "&nbsp;|&nbsp;".Length)
            {
                strResult = FunctionClass.LeftString(strResult, strResult.Length - "&nbsp;|&nbsp;".Length);
            }

            return strResult;
        }

        /// <summary>
        /// 替换选中分类标签
        /// </summary>
        /// <param name="myTag">自定义标签对象</param>
        /// <param name="strResult">HTML</param>
        /// <param name="strURL">当前URL</param>
        /// <returns>返回替换后HTML</returns>
        private string ReplaceSelTypeID(CustomTag myTag, string strResult, string strURL)
        {
            if (myTag.TagType == "products")
            {
                for (int i = 0; i < 30; i++)
                {
                    if (strURL.IndexOf("_products_" + i + "_") > -1)
                        strResult = HtmlFiles.ReplaceContent(strResult, "$selprotypeClass:" + i + "$", " selprotype");
                    else
                        strResult = HtmlFiles.ReplaceContent(strResult, "$selprotypeClass:" + i + "$", "");
                }
            }

            if (myTag.TagType == "news")
            {
                for (int i = 0; i < 30; i++)
                {
                    if (strURL.IndexOf("_news_" + i + "_") > -1)
                        strResult = HtmlFiles.ReplaceContent(strResult, "$selnewtypeClass:" + i + "$", " selnewtype");
                    else
                        strResult = HtmlFiles.ReplaceContent(strResult, "$selnewtypeClass:" + i + "$", "");
                }
            }

            return strResult;
        }

        /// <summary>
        /// 替换标签下子标签内容
        /// </summary>
        /// <param name="myDs">数据记录集</param>
        /// <param name="myPage">页面对象</param>
        private void ReplaceSubTags(DataSet myDs, PageContent myPage)
        {
            //查询是否有子标签，如果有遍历并读取数据
            if (myPage.HTML.IndexOf("<xy:") > -1)
            {
                CustomTags mySubTags = new CustomTags(myPage.HTML);
                ArrayList arlSubTags = mySubTags.Objects;
                for (int m = 0; m < arlSubTags.Count; m++)
                {
                    CustomTag mySubTag = (CustomTag)arlSubTags[m];
                    if (mySubTag.TagType == "news")
                    {
                        this.GetAllPagesHtml(mySubTags, mySubTag, myDs.Tables[4], myDs.Tables[5], myPage,
                    "NewType", "PID", "TypeCalled", "", "", false);
                    }
                    else
                    {
                        this.GetAllPagesHtml(mySubTags, mySubTag, myDs.Tables[6], myDs.Tables[7], myPage,
                    "TypeID", "PID", "TypeCalled", "", "", false);
                    }
                }
            }
        }
    }
}